package com.wgq.uitest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wgq.uitest.exp4.MyService;

public class Main3Activity extends AppCompatActivity implements View.OnClickListener {

    Button bind, status, unbind;
    Button calculate;
    EditText numa, numb;
    TextView Status, result;
    MyService.MyServiceBinder serviceBinder;
    boolean isServiceBind  = false;
    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            System.out.println("连接");
            serviceBinder = (MyService.MyServiceBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        bind = findViewById(R.id.bind);
        status = findViewById(R.id.status);
        unbind = findViewById(R.id.unbind);
        Status = findViewById(R.id.text);

        calculate = findViewById(R.id.calculate);
        numa = findViewById(R.id.Numbera);
        numb = findViewById(R.id.Numberb);
        result = findViewById(R.id.result);

        bind.setOnClickListener(this);
        status.setOnClickListener(this);
        unbind.setOnClickListener(this);
        calculate.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(Main3Activity.this, MyService.class);
        switch (v.getId()){
            case R.id.bind:
                if(!isServiceBind){
                    startService(intent);
                    bindService(intent, conn, BIND_AUTO_CREATE);
                    isServiceBind = true;
                    Status.setText("当前状态服务状态：连接成功");
                }
                break;
            case R.id.status:
                if(isServiceBind){
                    int status = serviceBinder.getCount();
                    Status.setText("当前状态服务状态：" + status);
                }
                else{
                    Status.setText("当前状态服务状态：未连接");
                }
                break;
            case R.id.unbind:
                if(isServiceBind){
                    unbindService(conn);
                    isServiceBind = false;
                    stopService(intent);
                    Status.setText("当前状态服务状态：断开连接");
                }
                break;
            case R.id.calculate:
                if(isServiceBind){
                    if(!numa.getText().toString().equals("") && !numb.getText().toString().equals("")){
                        String res = serviceBinder.getResult(numa.getText().toString(), numb.getText().toString()) + "";
                        result.setText("计算结果为：" + res);
                        Status.setText("当前状态服务状态：计算成功");
                    }
                    else{
                        result.setText("请输入要计算的数字！");
                    }
                }
                else{
                    Status.setText("当前状态服务状态：未连接");
                }
                break;
        }
    }
}
